﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;

namespace Assessment.Management.UI.Admin.UserControls.Statistic
{
    public partial class PollEdit : System.Web.UI.UserControl
    {
        //TODO: 23/04/14 Chuyển Button xóa ip lên toolbar
        #region events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    string id = GetParam();
                    if (!string.IsNullOrEmpty(id))
                    {
                        BindData(id);
                    }
                    else
                    {
                        GotoViewPage();
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            PollParam param = new PollParam(FunctionType.UpdatePoll);
            Poll poll = new Poll();
            poll.ID = hdID.Value;
            poll.Password = txtNewPassword.Text.Trim();
            poll.Version = int.Parse(hdVersion.Value);
            poll.UpdatedBy = SMX.smx_UpdatedBy;
            poll.UpdatedDTG = DateTime.Now;
            param.Poll = poll;
            MainController.Provider.Execute(param);
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=polldisplay&id=" + hdID.Value);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            PollParam param = new PollParam(FunctionType.DeletePoll);
            Poll poll = new Poll();
            poll.ID = hdID.Value;
            poll.Deleted = SMX.smx_IsDeleted;
            poll.Version = int.Parse(hdVersion.Value);
            poll.UpdatedBy = SMX.smx_UpdatedBy;
            poll.UpdatedDTG = DateTime.Now;
            param.Poll = poll;
            MainController.Provider.Execute(param);
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=polldisplay&id=" + hdID.Value);
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=pollsetting");
        }
        #endregion

        #region methods
        private void GotoViewPage()
        {
            Response.Redirect("/UI/Admin/UniAdmin.aspx?do=polldisplay&id=" + hdID.Value);
        }

        private void BindData(string id)
        {
            PollParam param = GetPollByID(id);
            BindItemToForm(param);
        }

        private void BindItemToForm(PollParam param)
        {
            txtOldPassword.Text = param.Poll.Password;
            lblTeacherName.Text = param.Poll.TeacherName;
            lblClassName.Text = param.Poll.ClassName;
            lblSubjectName.Text = param.Poll.SubjectName;
            hdVersion.Value = param.Poll.Version.ToString();
            hdID.Value = GetParam();
        }

        private PollParam GetPollByID(string id)
        {
            PollParam param = new PollParam(FunctionType.GetPollByID);
            param.Poll = new Poll() { ID = id };
            MainController.Provider.Execute(param);
            return param;
        }

        private string GetParam()
        {
            return Request.QueryString["id"];
        }
        #endregion
    }
}